package com.example.mtreader;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;

import java.util.HashMap;
import java.util.Iterator;

/**
 * 明泰读卡器处理类
 */
public class MainActivity {
    private Context context;
    private int dev = 0;
    private int fd = -1;

    static {
        System.loadLibrary("mt3x32");
        System.loadLibrary("DecodeWlt");
    }

    private boolean mtDeviceConnected;
    private boolean mtPortOpen;

    /**
     * 获取明泰读卡器连接状态
     * @param fd 文件描述符
     * @return 连接状态
     */
    public static native int mt8deviceopenfd(int fd);

    /**
     * 调用读取身份证jni方法
     * @param name name
     * @param sex sex
     * @param nation nation
     * @param birth birth
     * @param address address
     * @param idnum idnum
     * @param asigndepartment asigndepartment
     * @param datestart datestart
     * @param dateend dateend
     * @param rphotodatalen rphotodatalen
     * @param photodatainfo photodatainfo
     * @return 读取身份证状态
     */
    public static native int mt8IDCardRead(
            byte[] name,
            byte[] sex,
            byte[] nation,
            byte[] birth,
            byte[] address,
            byte[] idnum,
            byte[] asigndepartment,
            byte[] datestart,
            byte[] dateend,
            int[] rphotodatalen,
            byte[] photodatainfo);

    /**
     * 读取健康卡
     * @param name 名字
     * @param sex 性别
     * @param nation nation
     * @param iDCardNo 身份证号
     * @param birth 生日
     * @param departName departName
     * @param departTime departTime
     * @param validTime validTime
     * @param photo photo
     * @param addr1 addr1
     * @param addr2 addr2
     * @param lpErrMsg lpErrMsg
     * @return 读取状态值
     */
    public static native int ReadHealthCard(
            byte[] name,
            byte[] sex,
            byte[] nation,
            byte[] iDCardNo,
            byte[] birth,
            byte[] departName,
            byte[] departTime,
            byte[] validTime,
            byte[] photo,
            byte[] addr1,
            byte[] addr2,
            byte[] lpErrMsg);

    /**
     * mt8hexasc
     * @param hex hex
     * @param asc asc
     * @param len len
     * @return int
     */
    public static native int mt8hexasc(byte[] hex, byte[] asc, int len);

    /**
     * 构造器
     * @param context 上下文
     */
    public MainActivity(Context context) {
        this.context = context;
    }

    /**
     * 识别明泰读卡器USB接口，获取权限
     */
    public void getMTPermission() {
        UsbManager manager = (UsbManager) this.context.getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0,
                new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED), 0);
        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            if (device.getVendorId() == 0x23a4 && device.getProductId() == 0x0219) {
                manager.requestPermission(device, mPermissionIntent);
                UsbDeviceConnection connection = manager.openDevice(device);
                mtDeviceConnected = true;
                if (connection != null) {
                    fd = connection.getFileDescriptor();
                }
                break;
            }
            dev = mt8deviceopenfd(fd);
        }
    }

    /**
     * 重新获取权限
     */
    public void reGetMTPermission() {
        UsbManager manager = (UsbManager) this.context.getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0,
                new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED), 0);
        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            if (device.getVendorId() == 0x23a4 && device.getProductId() == 0x0219) {
                UsbDeviceConnection connection = manager.openDevice(device);
                mtDeviceConnected = true;
                if (connection != null) {
                    fd = connection.getFileDescriptor();
                }
                break;
            }
            dev = mt8deviceopenfd(fd);
        }
    }

    /**
     * 获取明泰读卡器连接状态
     * @return mtDeviceConnected
     */
    public boolean getMtDeviceConnect() {
        return mtDeviceConnected;
    }

    /**
     * 获取明泰读卡器端口状态
     * @return mtPortOpen
     */
    public boolean getMtPortState() {
        return mtPortOpen;
    }

    /**
     * 连接明泰读卡器
     */
    public void openDevice() {
        dev = mt8deviceopenfd(fd);
        if (dev > 0) {
            mtPortOpen = true;
        } else {
            mtPortOpen = false;
        }
    }

    /**
     * 设置明泰读卡器连接状态
     * @param deviceConnect 连接状态
     */
    public void setDeviceConnect(boolean deviceConnect) {
        this.mtDeviceConnected = deviceConnect;
    }

    /**
     * 设置明泰读卡器端口状态
     * @param mtPortOpen 端口状态
     */
    public void setMtPortOpen(boolean mtPortOpen) {
        this.mtPortOpen = mtPortOpen;
    }
}
